package top.huxy.boot.controller.admin;

import org.springframework.web.bind.annotation.*;
import top.huxy.boot.annotation.Master;
import top.huxy.boot.annotation.Operation;
import top.huxy.boot.annotation.RequestRemake;
import top.huxy.boot.model.entity.VideoApi;
import top.huxy.boot.service.impl.VideoApiServiceImpl;
import top.huxy.boot.util.obj.Result;

import java.util.Date;

/**
 * @Classname VideoApiController
 * @Description VideoApiController
 * @Date 2021/10/3 20:16
 * @Created by huxy
 */
@RestController
@RequestMapping("/admin/videoApi")
public class VideoApiController {

    VideoApiServiceImpl videoApiService;

    public VideoApiController(VideoApiServiceImpl videoApiService) {
        this.videoApiService = videoApiService;
    }

    @GetMapping("/getVideoApiList")
    @RequestRemake("管理员获取视频解析API")
    public Result getVideoApiList() {
        return Result.success(videoApiService.getVideoApiList());
    }

    @Operation("添加视频解析接口")
    @PostMapping("/add")
    @Master(Master.Role.NORMAL_ADMIN)
    public Result add(VideoApi videoApi) {
        videoApi.setCreateTime(System.currentTimeMillis());
        try{
            int res = videoApiService.add(videoApi);
            if (res == 1) {
                return Result.success("ok");
            } else {
                return Result.clientError("err:" + res);
            }
        }catch (Exception e){
            return Result.clientError("添加失败");
        }
    }

    @Operation("修改视频解析接口")
    @PostMapping("/update")
    @Master(Master.Role.NORMAL_ADMIN)
    public Result update(VideoApi videoApi) {
        videoApi.setUpdateTime(new Date());
        int res = videoApiService.update(videoApi);
        if (res == 1) {
            return Result.success("ok");
        } else {
            return Result.clientError("err:" + res);
        }
    }

    @Operation("删除视频解析接口")
    @PostMapping("/delete")
    @Master(Master.Role.NORMAL_ADMIN)
    public Result delete(@RequestParam("id") Integer id) {
        int res = videoApiService.delete(id);
        if (res == 1) {
            return Result.success("ok");
        } else {
            return Result.clientError("err:" + res);
        }
    }

    @PostMapping("/changeEnable")
    @Master(Master.Role.NORMAL_ADMIN)
    public Result changeEnable(@RequestParam("id") Integer id,@RequestParam("enable") Boolean enable) {
        int res = videoApiService.changeEnable(id,enable);
        if (res == 1) {
            return Result.success("ok");
        } else {
            return Result.clientError("err:" + res);
        }
    }

}
